package ltd.itlover.yqblog.blog;


import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sak.ultilviewlib.adapter.BaseHeaderAdapter;
import com.sak.ultilviewlib.util.MeasureTools;

import ltd.itlover.yqblog.R;

public class BlogRefreshHeaderAdapter extends BaseHeaderAdapter {

    private ImageView loadingImg;
    private TextView remindTextView;
    private int viewHeight;
    private float pull_distance=0;

    public BlogRefreshHeaderAdapter(Context context) {
        super(context);
    }

    //初始化 头部下拉要展示的布局 的阶段
    @Override
    public View getHeaderView() {
        View mView = mInflater.inflate(R.layout.header_refresh_layout, null, false);
        loadingImg = (ImageView) mView.findViewById(R.id.loading);
        remindTextView = (TextView) mView.findViewById(R.id.refresh_header_text);
        MeasureTools.measureView(mView);
        viewHeight = mView.getMeasuredHeight();
        return mView;
    }


    //下拉进行时
    @Override
    public void pullViewToRefresh(int deltaY) {
        //这里乘以0.3 是因为UltimateRefreshView 源码中对于滑动有0.3的阻尼系数，为了保持一致
        loadingImg.setImageResource(R.drawable.pull_icon);
        pull_distance=pull_distance+deltaY*0.3f;
        float scale = pull_distance / viewHeight;
        loadingImg.setScaleX(scale);
        loadingImg.setScaleY(scale);
        remindTextView.setText("下拉刷新");

    }


    //顶部的view 完全被拉下来
    @Override
    public void releaseViewToRefresh(int deltaY) {
        remindTextView.setText("正在刷新");
        loadingImg.setImageResource(R.drawable.mei_tuan_loading_pre);
        AnimationDrawable mAnimationDrawable= (AnimationDrawable) loadingImg.getDrawable();
        mAnimationDrawable.start();
    }


    //正在刷新进行时
    @Override
    public void headerRefreshing() {
        loadingImg.setImageResource(R.drawable.mei_tuan_loading);
        AnimationDrawable mAnimationDrawable= (AnimationDrawable) loadingImg.getDrawable();
        mAnimationDrawable.start();
    }

    //刷新完成
    @Override
    public void headerRefreshComplete() {

//        loadingImg.setImageResource(R.drawable.finishing_refresh);
//        AnimationDrawable mAnimationDrawable= (AnimationDrawable) loadingImg.getDrawable();
//        mAnimationDrawable.start();
        loadingImg.setScaleX(0);
        loadingImg.setScaleY(0);
        pull_distance=0;
    }


}
